package net.sx.minemodule.mvp.presenters;

import android.databinding.ObservableField;
import android.util.Log;
import android.view.View;

import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.StringUtil;
import com.kedll.library.util.ToastUtil;

import net.sx.minemodule.MineApiUtils;
import net.sx.minemodule.R;
import net.sx.minemodule.mvp.views.MineUpdateNickNameView;


public class MineUpdateNickNamePresenters extends BasePresenter<MineUpdateNickNameView> {
    /**
     * 手机号
     */
    public final ObservableField<String> nickName = new ObservableField<>();


    public MineUpdateNickNamePresenters(MineUpdateNickNameView view) {
        super(view);
    }

    public void onClickUpdateNick(View view) {
        if (checkNickNameData(view)) {
            oUpdateNick(view);
        }
    }

    private void oUpdateNick(View view) {
        MineApiUtils.modifyUserInfo("NickName", nickName.get(), this::updaterSuccess, throwable -> {

        });
    }

    private void updaterSuccess(Object object) {
        mView.updaterSuccessView(nickName.get());
    }

    /**
     * 检查登录所需数据的合法性
     *
     * @return
     */
    private boolean checkNickNameData(View view) {
         String name = nickName.get();
        //昵称不能为空
        if (StringUtil.isEmpty(name)) {
            ToastUtil.showSnackbar(view, "昵称" + view.getContext().getString(R.string.string_can_not_be_empty));
            return false;
        }
        if (name.length() < 4) {
            ToastUtil.showSnackbar(view, "使用中英文、数字或者特殊字符。字数限制在4-20位");
            return false;
        }
        return true;
    }
}
